<script setup>
import { CategoryList, AddCategory ,UpdateCategory,DeleteCategory} from "@/api/category.js";
import { Edit, Delete } from "@element-plus/icons-vue";
import { ref } from "vue";
import {ElMessage, ElMessageBox } from "element-plus";
const categorys = ref([
  {
    id: 3,
    categoryName: "美食",
    categoryAlias: "my",
    createTime: "2023-09-02 12:06:59",
    updateTime: "2023-09-02 12:06:59",
  },
  {
    id: 4,
    categoryName: "娱乐",
    categoryAlias: "yl",
    createTime: "2023-09-02 12:08:16",
    updateTime: "2023-09-02 12:08:16",
  },
  {
    id: 5,
    categoryName: "军事",
    categoryAlias: "js",
    createTime: "2023-09-02 12:08:33",
    updateTime: "2023-09-02 12:08:33",
  },
]);
const categoryList = async () => {
  let data = await CategoryList();
  categorys.value = data.data;
  console.log(data);
};
//控制添加分类弹窗
const dialogVisible = ref(false);
//添加分类数据模型
const categoryModel = ref({
  categoryName: "",
  categoryAlias: "",
});
//添加分类表单校验
const title = ref("");

const rules = {
  categoryName: [
    { required: true, message: "请输入分类名称", trigger: "blur" },
  ],
  categoryAlias: [
    { required: true, message: "请输入分类别名", trigger: "blur" },
  ],
};

const AddForCategory = async () => {
  const response = await AddCategory(categoryModel.value);
  ElMessage.success("添加成功！");
  console.log(response);
  categoryList();
  dialogVisible.value = false;
};
const UpdateForCategory = async () => {
  const response = await UpdateCategory(categoryModel.value);
  ElMessage.success("修改成功！");
  console.log(response);
  await categoryList();
  dialogVisible.value = false;
};
const showEditDialogue = (row) => {
  title.value = "编辑分类";
  console.log(title.value);
  dialogVisible.value = true;
  categoryModel.value.categoryName = row.categoryName;
  categoryModel.value.categoryAlias = row.categoryAlias;
  categoryModel.value.id = row.id;
};
const showADDDialogue = () => {
  title.value = "添加分类";
  dialogVisible.value = true;
  categoryModel.value.categoryName = "";
  categoryModel.value.categoryAlias = "";
};
const DeleteForCategory = (row) => {
  ElMessageBox.confirm(
      '此操作将删除分类和笔记，是否确认删除？',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        await DeleteCategory(row.id);
        await categoryList();
        ElMessage({
          type: 'success',
          message: '删除成功！',
        });
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '删除取消!',
        })
      })
}
categoryList();
</script>


<template>
  <el-card class="page-container">
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form
        :model="categoryModel"
        :rules="rules"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-form-item label="分类名称" prop="categoryName">
          <el-input
            v-model="categoryModel.categoryName"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="categoryAlias">
          <el-input
            v-model="categoryModel.categoryAlias"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="title==='添加分类'?AddForCategory():UpdateForCategory()"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>

    <template #header>
      <div class="header">
        <span>笔记分类</span>
        <div class="extra">
          <el-button type="primary" @click="showADDDialogue"
            >添加分类</el-button
          >
        </div>
      </div>
    </template>

    <el-table :data="categorys" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="categoryName"></el-table-column>
      <el-table-column label="分类别名" prop="categoryAlias"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="showEditDialogue(row)"
          ></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="DeleteForCategory(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
  </el-card>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.el-card {
  background: linear-gradient(135deg, #f8fafc 0%, #e0e7ff 100%);
  border-radius: 16px;
  box-shadow: 0 4px 24px 0 rgba(80, 120, 200, 0.08);
  padding: 32px 24px 24px 24px;
  margin-top: 30px;
  transition: box-shadow 0.3s;
}
.el-card:hover {
  box-shadow: 0 8px 32px 0 rgba(80, 120, 200, 0.18);
}
.header {
  font-size: 1.5rem;
  font-weight: 600;
  color: #3b3f5c;
  letter-spacing: 1px;
  margin-bottom: 18px;
}
.extra .el-button {
  border-radius: 20px;
  font-weight: 500;
  letter-spacing: 1px;
  background: linear-gradient(90deg, #6366f1 0%, #60a5fa 100%);
  color: #fff;
  border: none;
  box-shadow: 0 2px 8px 0 rgba(99,102,241,0.10);
  transition: background 0.2s;
}
.extra .el-button:hover {
  background: linear-gradient(90deg, #60a5fa 0%, #6366f1 100%);
}
.el-table {
  border-radius: 12px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 2px 8px 0 rgba(99,102,241,0.04);
}
.el-table th {
  background: #f1f5f9;
  color: #6366f1;
  font-weight: 600;
  font-size: 1rem;
}
.el-table .el-button {
  transition: transform 0.15s;
}
.el-table .el-button:hover {
  transform: scale(1.12);
  box-shadow: 0 2px 8px 0 rgba(99,102,241,0.10);
}
.el-empty {
  margin: 32px 0;
}
</style>
